package com.mzy.linear_struct.hot100.回溯;

import java.io.DataInput;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/09/08/下午7:54
 * @Description:
 */
public class Pro17电话号码的字母组合 {
    public List<String> letterCombinations(String digits) {
        ArrayList<String> ans = new ArrayList<>();
        if (digits.isEmpty()) return ans;
        StringBuilder path = new StringBuilder();
        backtracking(digits.toCharArray(), ans, 0, path);
        return ans;
    }

    private void backtracking(char[] digits, ArrayList<String> ans, int index, StringBuilder path) {
        if (digits.length == index) {
            ans.add(path.toString());
            return;
        }

        char[] letter = covertLetter(digits[index]).toCharArray();

        for (int i = 0; i < letter.length; i++) {
            path.append(letter[i]);
            backtracking(digits, ans, index + 1, path);
            path.deleteCharAt(path.length() - 1);
        }
    }

    private static String covertLetter(char digits) {

        if (digits == '2') {
            return "abc";
        } else if (digits == '3') {
            return "def";
        } else if (digits == '4') {
            return "ghi";
        } else if (digits == '5') {
            return "jkl";
        } else if (digits == '6') {
            return "mno";
        } else if (digits == '7') {
            return "pqrs";
        } else if (digits == '8') {
            return "tuv";
        } else if (digits == '9') {
            return "wxyz";
        } else {
            return "";
        }

    }
}






























